package com.example.myapplication;

import java.util.ArrayList;

public class ChessBoard extends ChessBaseItem{
    ArrayList<ChessPiece> onlinePieces = new ArrayList();
    private int[][] boardArray;

    public ChessBoard(int width, int height) {
        super(width, height);
        this.boardArray = new int[this.height][this.width];
    }

    public boolean addPiece(ChessPiece piece) {
        this.onlinePieces.add(piece);
        if (!this.setLayout()) {
            this.removePiece(piece.getId());//
            return false;
        } else {
            return true;
        }
    }
    public boolean removePiece(String id) {
        for(int i = 0; i < this.onlinePieces.size(); ++i) {
            if (((ChessPiece)this.onlinePieces.get(i)).getId() == id) {
                this.onlinePieces.remove(i);
                this.setLayout();
                return true;
            }
        }

        return false;
    }

    public Position movePiece(String id, Position pos) {
        ChessPiece piece = this.getPiece(id);
        if (!piece.move(pos)) {
            piece.undo();
            return null;
        } else if (!this.setLayout()) {
            piece.undo();
            return null;
        } else {
            return pos;
        }
    }

    public Position movePiece(String id) {
        ArrayList<Position> useablePositions = new ArrayList();
        ArrayList<Position> allPositions = new ArrayList();
        ChessPiece piece = this.getPiece(id);
        if (piece == null) {
            return null;
        } else {
            Position curr_pos = piece.getPosition();
            allPositions.add(new Position(curr_pos.getPosx(), curr_pos.getPosy() - 1));
            allPositions.add(new Position(curr_pos.getPosx(), curr_pos.getPosy() + 1));
            allPositions.add(new Position(curr_pos.getPosx() - 1, curr_pos.getPosy()));
            allPositions.add(new Position(curr_pos.getPosx() + 1, curr_pos.getPosy()));
            int i;
            if (piece.getPrevPosition() != null) {
                for(i = 0; i < allPositions.size(); ++i) {
                    if (!((Position)allPositions.get(i)).equals(piece.getPrevPosition())) {
                        useablePositions.add((Position)allPositions.get(i));
                    }
                }

                useablePositions.add(piece.getPrevPosition());
            } else {
                useablePositions = allPositions;
            }

            for(i = 0; i < useablePositions.size(); ++i) {
                if (this.movePiece(id, (Position)useablePositions.get(i)) != null) {
                    return (Position)useablePositions.get(i);
                }
            }

            return null;
        }
    }

    public ArrayList<ChessPiece> getAllPieces() {
        return this.onlinePieces;
    }

    public void draw() {
        System.out.printf("draw board:%d X %d\n", this.width, this.height);

        int i;
        for(i = 0; i < this.boardArray.length; ++i) {
            for(int j = 0; j < this.boardArray[0].length; ++j) {
                System.out.printf("%d\t", this.boardArray[i][j]);
            }

            System.out.print("\n");
        }

        for(i = 0; i < this.onlinePieces.size(); ++i) {
            ChessPiece piece = (ChessPiece)this.onlinePieces.get(i);
            System.out.println(piece.toString());
        }

    }

    @Override
    public boolean setLayout() {
        int[][] prevBoardArray = Utils.deepCopyInt(this.boardArray);
        this.boardArray = new int[this.height][this.width];

        for(int i = 0; i < this.onlinePieces.size(); ++i) {
            ChessPiece piece = (ChessPiece)this.onlinePieces.get(i);
            if (piece.getPosition().getPosx() + piece.getWidth() > this.width || piece.getPosition().getPosy() + piece.getHeight() > this.height || piece.getPosition().getPosx() < 0 || piece.getPosition().getPosy() < 0) {
                return false;
            }

            for(int posy = piece.getPosition().getPosy(); posy < piece.getPosition().getPosy() + piece.getHeight(); ++posy) {
                for(int posx = piece.getPosition().getPosx(); posx < piece.getPosition().getPosx() + piece.getWidth(); ++posx) {
                    if (this.boardArray[posy][posx] != 0) {
                        this.boardArray = prevBoardArray;
                        return false;
                    }

                    this.boardArray[posy][posx] = 1;
                }
            }
        }

        return true;
    }
    public int[][] getLayoutArray() {
        return Utils.deepCopyInt(this.boardArray);
    }

    private ChessPiece getPiece(String id) {
        for(int i = 0; i < this.onlinePieces.size(); ++i) {
            if (((ChessPiece)this.onlinePieces.get(i)).getId() == id) {
                return (ChessPiece)this.onlinePieces.get(i);
            }
        }

        return null;
    }
}
